package com.moyu.myadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moyu.myadmin.dao.entity.SysPermissionEntity;
import com.moyu.myadmin.dao.mapper.SysPermissionMapper;
import com.moyu.myadmin.model.convert.SysPermissionConvert;
import com.moyu.myadmin.model.dto.SysPermissionDTO;
import com.moyu.myadmin.model.vo.SysMenuVO;
import com.moyu.myadmin.model.vo.SysPermissionVO;
import com.moyu.myadmin.model.vo.SysRolePermissionVO;
import com.moyu.myadmin.service.SysMenuService;
import com.moyu.myadmin.service.SysPermissionService;
import com.moyu.myadmin.utils.PageConvert;
import com.moyu.myadmin.utils.QueryData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermissionEntity> implements SysPermissionService {

    private final SysMenuService menuService;

    @Autowired
    public SysPermissionServiceImpl(SysMenuService menuService) {
        this.menuService = menuService;
    }

    @Override
    public Page<SysPermissionVO> queryListPage(QueryData<SysPermissionDTO> queryData) {
        Page<SysPermissionEntity> page = new Page<>(queryData.getPageNum(), queryData.getPageSize());
        page = baseMapper.queryListPage(page, queryData.getData());
        List<SysPermissionVO> voList = SysPermissionConvert.INSTANCE.toVoList(page.getRecords());
        return PageConvert.convert(page, voList);
    }

    @Override
    public boolean save(SysPermissionDTO dto) {
        SysPermissionEntity entity = SysPermissionConvert.INSTANCE.dtoToEntity(dto);
        return this.saveOrUpdate(entity);
    }

    @Override
    public boolean saveOrUpdateBatch(List<SysPermissionDTO> list) {
        // 先删除老数据，再新增新数据
        this.removeByRoleId(list.get(0).getRoleId());
        List<SysPermissionEntity> insertList = SysPermissionConvert.INSTANCE.dtoEntityList(list);
        return this.saveOrUpdateBatch(insertList);
    }

    @Override
    public SysRolePermissionVO queryPermissionByRoleId(String roleId) {
        List<SysPermissionVO> permissionVOList = this.getPermissionByRoleId(roleId);
        List<SysMenuVO> menuVOList = menuService.queryAllList();
        return SysRolePermissionVO.builder().permissionList(permissionVOList).menuVOList(menuVOList).build();
    }

    @Override
    public boolean removeByRoleId(String roleId) {
        QueryWrapper<SysPermissionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysPermissionEntity::getRoleId, roleId);
        int delete = baseMapper.delete(queryWrapper);
        return delete > 0;
    }

    public List<SysPermissionVO> getPermissionByRoleId(String roleId) {
        List<SysPermissionEntity> list = this.lambdaQuery().eq(SysPermissionEntity::getRoleId, roleId).list();
        return SysPermissionConvert.INSTANCE.toVoList(list);
    }
}